package com.bee.web.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bee.web.annotation.ValidateToken;
import com.bee.web.service.SalerService;
import com.bee.web.util.FastJsonUtil;
import com.bee.web.util.Page;
import com.bee.web.vo.BaseVo;
import com.bee.web.vo.bean.SalerInfoBean;

@Controller
@RequestMapping("/saler")
public class SalerController extends BaseController {

	private Logger logger = Logger.getLogger(SalerController.class);

    @Autowired
    private SalerService salerService;

    @ValidateToken
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo search(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int rows,
            @RequestBody(required = false) SalerInfoBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
    	try {
            BaseVo vo = reloadData(page, rows, bean);
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
		} catch (Exception e) {
			e.printStackTrace();
            return new BaseVo("error", "服务器异常");
		}
    }
    
    @ValidateToken
    @RequestMapping(value = "/switchStatus", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo switchStatus(
            @RequestParam(required = false) int page,
            @RequestParam(required = false) int rows,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String status,
            @RequestBody(required = false) SalerInfoBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
    	try {
    		salerService.switchStatus(userId, status);
            BaseVo vo = reloadData(page, rows, bean);
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
		} catch (Exception e) {
			e.printStackTrace();
            return new BaseVo("error", "服务器异常");
		}
    }
    
    @ValidateToken
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo delete(
            @RequestParam(required = false) int page,
            @RequestParam(required = false) int rows,
            @RequestParam(required = false) String userId,
            @RequestBody(required = false) SalerInfoBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
        try {
        	salerService.delete(userId);
            BaseVo vo = reloadData(page, rows, bean);
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BaseVo("fail", "服务器异常");
    }
    
    @ValidateToken
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo saveOrUpdate(
            @RequestParam(required = false) int page,
            @RequestParam(required = false) int rows,
            @RequestBody(required = false) SalerInfoBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
    	try {
    		salerService.saveOrUpdate(bean);
            BaseVo vo = reloadData(page, rows, new SalerInfoBean());
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
		} catch (Exception e) {
            e.printStackTrace();
		}
        return new BaseVo("fail", "服务器异常");
    }
    
    @ValidateToken
    @RequestMapping(value = "/isExists", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo isExists(@RequestParam(required = false) String username) {
    	logger.info("[参数]" + FastJsonUtil.toJSON(username));
    	try {
    		BaseVo vo = null;
    		if (salerService.isExists(username)) {
    			vo = new BaseVo("yes", "用户名已存在");
			} else {
				vo = new BaseVo("no", "用户名可用");
			}
    		logger.info("[返回]" + FastJsonUtil.toJSON(vo));
    		return vo;
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return new BaseVo("fail", "服务器异常");
    }
    
    private BaseVo reloadData(int page, int rows, SalerInfoBean bean) throws Exception {
    	Page pager = new Page(page, rows);
    	int total = salerService.searchCount(bean, pager);
        List<SalerInfoBean> list = salerService.searchList(bean, pager);
        BaseVo vo = new BaseVo(page, rows);
        vo.setData(list);
        vo.setCount(list.size());
        vo.setTotalCount(total);
        return vo;
    }
    
}
